
$netdir=$Env:windir
$netinstall=$netdir+'\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe'

$servicesBase = 'C:\ICNET\Services\'

if(!(Test-Path $servicesBase))
{
  md $servicesBase
}

#Copy resources for services
if(Test-Path "C:\ICNET\Resources")
{
 Remove-Item C:\ICNET\Resources -Force -Recurse
}
md C:\ICNET\Resources
Copy-Item .\Deploy\Resources\* C:\ICNET\Resources -Force -Recurse

Function UpdateService($serviceName)
{
  $serviceLocation = $servicesBase + $serviceName
  $serviceExe = $serviceLocation + '\' + $serviceName + '.exe'
  
  # Stop & Remove Old Services
  if(Test-Path $serviceExe)
  {
    Write-Host "###### Killing $serviceName ######"
    & net stop $serviceName

    Write-Host "###### Uninstalling old $serviceName ######"
	  & $netinstall /u $serviceExe
	  Remove-Item $serviceLocation -Force -Recurse
  }

  # Installing service
  md $serviceLocation
  cp .\Deploy\Services\$serviceName\*.* $serviceLocation -Force

  Write-Host "###### Installing new $serviceName ######"
  & $netinstall $serviceExe

  # for some reason StaleCacheDeletionService is named CacheDeletionService in the service registry
  if($serviceName -eq "StaleCacheDeletionService") { $serviceName = 'CacheDeletionService' }

  Write-Host "###### Starting $serviceName ######"
  & net start $serviceName
}

UpdateService DEProcessing
UpdateService ContentProcessing
UpdateService ContentFeedProcessing
UpdateService FederationProcessing
UpdateService HistoryProcessing
UpdateService StaleCacheDeletionService


Write-Host "Done, press any key to continue ..."

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")